package cn.gcjxsc.service.integral;

import cn.gcjxsc.data.query.salesman.IntegralRecordQuery;
import cn.gcjxsc.data.vo.salesman.statics.IntegralRecordSumVo;
import cn.gcjxsc.data.vo.salesman.statics.IntegralRecordVo;
import cn.gcjxsc.entity.member.Member;
import cn.gcjxsc.entity.merchant.Store;
import cn.gcjxsc.entity.salesman.IntegralRecord;
import cn.gcjxsc.repository.salesaman.IntegralRecordRepository;
import cn.gcjxsc.repository.specs.SalesmanSpec;
import cn.gcjxsc.service.member.MemberService;
import cn.gcjxsc.service.merchant.StoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author pumbaa
 * @since 1.0
 */
@Service
public class IntegralRecordService {
    @Autowired
    private IntegralRecordRepository recordRepository;
    @Autowired
    private StoreService storeService;
    @Autowired
    private MemberService memberService;

    @Transactional
    public void saveRecord(IntegralRecord record) {
        recordRepository.save(record);
    }

    /**
     * 查询指定会员创建的店铺收益汇总
     *
     * @param refMemberId 业务对应的会员id
     * @param memberId    会员id
     * @return
     */
    public List<IntegralRecordSumVo> findMemberStoreStatics(Integer refMemberId, Integer memberId) {
        List<IntegralRecord> integralRecords = recordRepository.sumBySourceAndMemberId(String.valueOf(memberId), refMemberId);
        List<IntegralRecordSumVo> voList = new ArrayList<>();
        for (IntegralRecord integralRecord : integralRecords) {
            IntegralRecordSumVo vo = new IntegralRecordSumVo();
            vo.setStoreId(integralRecord.getStoreId());
            vo.setAmount(integralRecord.getAmount());
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 查询业务收益列表
     *
     * @param filter   过滤条件
     * @param page     当前页
     * @param pageSize 每页记录数
     * @return
     */
    public Page<IntegralRecord> findList(IntegralRecordQuery filter, int page, int pageSize) {
        filter.format();
        Pageable pageRequest = new PageRequest(page - 1, pageSize, new Sort(Sort.Direction.DESC, "created"));
        return recordRepository.findAll(SalesmanSpec.buildSalesmanFilter(filter), pageRequest);
    }

    /**
     * 数据转换
     *
     * @param list 收益列表
     * @return
     */
    public List<IntegralRecordVo> convertList(List<IntegralRecord> list) {
        List<IntegralRecordVo> voList = new ArrayList<>();
        if (list.size() > 0) {
            Set<Integer> storeIds = new HashSet<>();
            Set<Integer> memberIdSet = new HashSet<>();
            for (IntegralRecord integralRecord : list) {
                storeIds.add(integralRecord.getStoreId());
                memberIdSet.add(Integer.valueOf(integralRecord.getSource()));

                IntegralRecordVo vo = new IntegralRecordVo();
                BeanUtils.copyProperties(integralRecord, vo);
                vo.setSalesmanId(integralRecord.getSalesman().getId());
                vo.setSalesmanName(integralRecord.getSalesman().getName());
                vo.setType(integralRecord.getType().getType());
                voList.add(vo);
            }
            //查询店铺信息
            ArrayList<Integer> ids = new ArrayList<>(storeIds);
            List<Store> storeList = storeService.findByIds(ids);
            for (Store store : storeList) {
                for (IntegralRecordVo vo : voList) {
                    if (Objects.equals(store.getId(), vo.getStoreId())) {
                        vo.setStoreName(store.getName());
                    }
                }
            }
            ArrayList<Integer> memberIds = new ArrayList<>(memberIdSet);
            //查询会员信息
            List<Member> memberList = memberService.findByIds(memberIds);
            if (memberList.size() > 0) {
                for (Member member : memberList) {
                    for (IntegralRecordVo integralRecordVo : voList) {
                        if (Objects.equals(Integer.valueOf(integralRecordVo.getSource()), member.getId())) {
                            integralRecordVo.setSourceName(member.getUsername());
                        }
                    }
                }
            }
        }
        return voList;
    }
}
